package solution._0000._0002;

import common.ListNode;

/**
 * Created by chen_snow
 * Date: 2021/4/26 20:59
 * Description:
 */
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if (null == l1) {
            return l2;
        }

        if (null == l2) {
            return l1;
        }

        return addTwoNumbers(l1, l2, 0);
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2, int plus) {
        if (null == l1 && null == l2 && plus == 0) {
            return null;
        }

        int first = null == l1 ? 0 : l1.val;
        int second = null == l2 ? 0 : l2.val;

        int tempPlus = (first + second + plus) / 10;
        int tempResult = (first + second + plus) - tempPlus * 10;

        ListNode l1Next = null == l1 ? null : l1.next;
        ListNode l2Next = null == l2 ? null : l2.next;

        ListNode result = new ListNode(tempResult);
        result.next = addTwoNumbers(l1Next, l2Next, tempPlus);
        return result;
    }
}
